#!/bin/bash
#
# 4shared.com module
# Copyright (c) 2010-2012 Plowshare team
#
# This file is part of Plowshare.
#
# Plowshare is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plowshare is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Plowshare.  If not, see <http://www.gnu.org/licenses/>.

MODULE_1CHANNEL_CH_REGEXP_URL="http://\(www\.\)\?1channel\.ch/"

MODULE_1CHANNEL_CH_DOWNLOAD_OPTIONS=""
MODULE_1CHANNEL_CH_DOWNLOAD_RESUME=yes
MODULE_1CHANNEL_CH_DOWNLOAD_FINAL_LINK_NEEDS_COOKIE=yes

MODULE_1CHANNEL_CH_LIST_OPTIONS=""

1channel_ch_list() {

local URL=$1



formatarraylinks ()
{
  for (( i=0; i < ${#Links[@]}; i++ )) do
      Links[i]+=$'\n'
  done
}

process_putlocker()
{
    finallink=$(curl -e $URL -i "${Links[${putlocker[k]}]}" | grep Location | replace 'Location: ' '')
}

process_sockshare()
{
    finallink=$(curl -e $URL -i "${Links[${sockshare[k]}]}" | grep Location | replace 'Location: ' '')
}

initarray ()
{
  for (( i=0; i < ${#Sites[@]}; i++ )) do
      Sites[i]=${Sites[i]:141}
      Sites[i]=${Sites[i]%?????????????????????????}
      Sites[i]+=$'\n'
  done
  for (( j=0; j < ${#Links2nd[@]}; j++ )) do
      if [[ $(echo "${Links2nd[$j]}") == *"Part 2"* ]] ; then
      #  echo "$i"
        formatarraylinks
         # Links=( ${Links[@]:0:$((i))} ${Links[@]:$((i))} )
        Links=( ${Links[@]:0:$j} ${Links[@]:$((j+1))} )
        formatarraylinks
       # echo ${Links[15]}
      fi
      if [[ $(echo "${Links2nd[$j]}") == *"Part 3"* ]] ; then
        formatarraylinks
        Links=( ${Links[@]:0:$((j-1))} ${Links[@]:$((j))} )
        formatarraylinks
      fi
  done
} 

formatarray ()
{
  for (( i=0; i < ${#Sites[@]}; i++ )) do
      Sites[i]+=$'\n'
  done
} 

stripurl=$( echo "$URL" | replace 'http://' '' )
numofslashes=${stripurl//[^\/]/}
numofslashes=${#numofslashes}

if [[ $numofslashes != "1" ]] ; then

	Pageinit=$(curl "$URL") || return

	IFS=$'\n'
	
	if [[ $(echo "$Pageinit" | grep 'external.php') = '' ]]; then
    return $ERR_LINK_DEAD
	fi
	
	Links=( $(echo "$Pageinit" | grep 'external.php' | parse_all_attr "href" ) )
	Links2nd=( $(echo "$Pageinit" | grep 'external.php' ) )
#	echo ${#Links2nd[@]} > links2ndraw
	Sites=( $(echo "$Pageinit" | grep 'document.writeln') )
	
  
	initarray
	for (( i=1; i < $((${#Sites[@]}+1)); i++ )) do
       if match 'Sponsor' "${Sites[$((i-1))]}"; then
          if [ $i = 1 ]; then
            Sites=( ${Sites[@]:1} )
            formatarray
            i=0
          fi
          if [[ $i > 1 ]]; then
            Sites=(${Sites[@]:0:$((i-1))} ${Sites[@]:$(($i))})
            formatarray
            i=0
          fi
       fi
  done
  
  
 for (( i=0; i < ${#Links[@]}; i++ )) do
      Links[i]=$( echo "${Links[i]}" | strip)
      Links[i]="http://www.1channel.ch${Links[i]}"
      Links[i]=$( echo "${Links[i]}" | strip)
      Links[i]+=$'\n'
      # Links[i]=$(echo -e "${Links[i]}'\n'")
 done
 
  # Sites in order of preference zalaa.com,uploadc.com,putlocker,sockshare,filenuke.com,filebox.com,
a=0
b=0
c=0
d=0
e=0
f=0
g=0
h=0
l=0
m=0
 for (( i=0; i < ${#Sites[@]}; i++ )) do
      if [[ "${Sites[i]}" == *"veehd.com"* ]]; then
        veehd[a]=$i
        a+=1
      elif [[ "${Sites[i]}" == *"stagevu.com"* ]]; then
        stagevu[b]=$i
        b+=1
      elif [[ "${Sites[i]}" == *"zalaa.com"* ]]; then
        zalaa[c]=$i
        c+=1
      elif [[ "${Sites[i]}" == *"uploadc.com"* ]]; then
        uploadc[d]=$i
        d+=1
      elif [[ "${Sites[i]}" == *"putlocker.com"* ]]; then
        putlocker[e]=$i
        e+=1
      elif [[ "${Sites[i]}" == *"sockshare.com"* ]]; then
        sockshare[f]=$i
        f+=1
      elif [[ "${Sites[i]}" == *"filebox.com"* ]]; then
        filebox[g]=$i
        g+=1
      elif [[ "${Sites[i]}" == *"daclips.com"* ]] || [[ "${Sites[i]}" == *"gorillavid.in"* ]]; then
        daclips[h]=$i
        h+=1
      elif [[ "${Sites[i]}" == *"vidbux.com"* ]]; then
        vidbux[l]=$i
        l+=1
      elif [[ "${Sites[i]}" == *"nosvideo.com"* ]]; then
        nosvideo[m]=$i
        m+=1
      fi
  done
  # echo ${daclips[@]} > daclips
  #  echo ${vidbux[@]} > vidbux
  #  echo "${Sites[@]}" > sitelist
  #   echo "${Links[@]}" > linklist
  #    echo "${Links[${daclips[0]}]}" > chosenlink
  # echo ${sockshare[@]} > sockshare
linknum=''
while [[ $linknum == '' ]]; do
  if [[ ${veehd[0]} != '' ]]; then
    for (( k=0; k < ${#veehd[@]}; k++ )) do
    linkfinal=$(plowdown -c $(curl -e "$URL" "${Links[${veehd[$k]}]}" | grep src | tail -1 | parse_attr 'src'))
      if [ $(echo $linkfinal) != '' ]; then
        linknum=${veehd[k]}
        break
      fi
    done
    unset veehd[@]
  elif [[ ${stagevu[0]} != '' ]]; then
    for (( k=0; k < ${#stagevu[@]}; k++ )) do
    linkfinal=$(plowdown -c $(curl -e "$URL" "${Links[${stagevu[$k]}]}" | grep src | tail -1 | parse_attr 'src'))
      if [[ $(echo $linkfinal) != '' ]]; then
        linknum=${stagevu[k]}
        break
      fi
    done
    unset stagevu[@]
  elif [[ ${uploadc[0]} != '' ]]; then
    for (( k=0; k < ${#uploadc[@]}; k++ )) do
    linkfinal=$(plowdown -c $(curl -e "$URL" "${Links[${uploadc[$k]}]}" | grep src | tail -1 | parse_attr 'src'))
      if [[ $(echo $linkfinal) != '' ]]; then
        linknum=${uploadc[k]}
        break
      fi
    done
    unset uploadc[@]
  elif [[ ${zalaa[0]} != '' ]]; then
    for (( k=0; k < ${#zalaa[@]}; k++ )) do
    linkfinal=$(plowdown -c $(curl -e "$URL" "${Links[${zalaa[$k]}]}" | grep src | tail -1 | parse_attr 'src'))
      if [[ $(echo $linkfinal) != '' ]]; then
        linknum=${zalaa[k]}
        break
      fi
    done
    unset zalaa[@]
  elif [[ ${nosvideo[0]} != '' ]]; then
    for (( k=0; k < ${#nosvideo[@]}; k++ )) do
    linkfinal=$(plowdown -c $(curl -e "$URL" "${Links[${nosvideo[$k]}]}" | grep src | tail -1 | parse_attr 'src'))
      if [[ $(echo $linkfinal) != '' ]]; then
        linknum=${nosvideo[k]}
        break
      fi
    done
    unset nosvideo[@]
  elif [[ ${sockshare[0]} != '' ]]; then
    for (( k=0; k < ${#sockshare[@]}; k++ )) do
      process_sockshare
      linkfinal=$(plowdown -c "$finallink")
      if [[ $(echo $linkfinal) != '' ]]; then
        linknum=${sockshare[k]}
        socksharedwn=$linknum
        break
      fi
      linkfinal=''
    done
    unset sockshare[@]
  elif [[ ${putlocker[0]} != '' ]]; then
    for (( k=0; k < ${#putlocker[@]}; k++ )) do
      process_putlocker
      linkfinal=$(plowdown -c "$finallink")
      if [[ $(echo $linkfinal) != '' ]]; then
        linknum=${putlocker[k]}
        putlockerdwn=$linknum
        break
      fi
      linkfinal=''
    done
    unset putlocker[@]
  elif [[ ${filebox[0]} != '' ]]; then
    for (( k=0; k < ${#filebox[@]}; k++ )) do
    linkfinal=$(plowdown -c $(curl -e "$URL" "${Links[${filebox[$k]}]}" | grep src | tail -1 | parse_attr 'src'))
      if [[ $(echo $linkfinal) != '' ]]; then
        linknum=${filebox[k]}
        break
      fi
    done
    unset filebox[@]
  elif [[ ${daclips[0]} != '' ]]; then
    for (( k=0; k < ${#daclips[@]}; k++ )) do
     linkfinal=$(plowdown -c $(curl -e "$URL" "${Links[${daclips[$k]}]}" | grep src | tail -1 | parse_attr 'src'))
      if [[ $(echo $linkfinal) != '' ]]; then
        linknum=${daclips[k]}
        break
      fi
    done
    unset daclips[@]
  elif [[ ${vidbux[0]} != '' ]]; then
    for (( k=0; k < ${#vidbux[@]}; k++ )) do
     linkfinal=$(plowdown -c $(curl -e "$URL" "${Links[${vidbux[$k]}]}" | grep src | tail -1 | parse_attr 'src'))
      if [[ $(echo $linkfinal) != '' ]]; then
        linknum=(${vidbux[k]})
        break
      fi
    done
    unset vidbux[@]
  elif [[ 0 == 0 ]]; then
    return $ERR_LINK_DEAD
    break
  fi  
done

  if [[ $linknum == $putlockerdwn ]]; then
    FILE_URL=$finallink
  elif [[ $linknum == $socksharedwn ]]; then
    FILE_URL=$finallink
  else
    finallink=$(curl -e "$URL" "${Links[linknum]}" | grep src | tail -1 | parse_attr 'src')
    FILE_URL=$(echo "$finallink")
  fi
  
     
    # echo "$linknum" > linknum
    # echo "${zalaa[0]}" > zalaa0
    # echo "linknum=$linknum putlocker=$putlocker sockshare=$sockshare"
	
	echo "$FILE_URL"
	
else

  stripurl=$( echo $URL | replace '1channel.ch' '')
  pagenum=$(echo $stripurl | sed 's/[^0-9]//g' )
  
  IFS=$'\n'
  Sitelist=($(curl $URL | grep tv-$pagenum ))
  formatsitelist ()
  {
    for (( i=0; i < ${#Sitelist[@]}; i++ )) do
        Sitelist[i]+=$'\n'
    done
  }
  for (( j=1; j < ${#Sitelist[@]}+1; j++ )) do
      if [[ $(echo "${Sitelist[$((j-1))]}" | grep '<h2>' ) != '' ]] || [[ $(echo "${Sitelist[$((j-1))]}" | grep 'transp2' ) != '' ]]; then
        if [[ $j == 1 ]]; then
          while [[ $(echo "${Sitelist[$((j-1))]}" | grep '<h2>' ) != '' ]] || [[ $(echo "${Sitelist[$((j-1))]}" | grep 'transp2' ) != '' ]]; do
            Sitelist=( ${Sitelist[@]:1} )
          done
        else
          while [[ $(echo "${Sitelist[$((j-1))]}" | grep '<h2>' ) != '' ]] || [[ $(echo "${Sitelist[$((j-1))]}" | grep 'transp2' ) != '' ]]; do
            formatsitelist
            Sitelist=(${Sitelist[@]:0:$((j-1))} ${Sitelist[@]:$(($j))})
          done
      fi
    fi
  done
  for (( j=1; j < ${#Sitelist[@]}; j++ )) do
    # test "${Sitelist[j]}" || continue
    FILE_URL=$(echo "${Sitelist[j]}")
    FILE_URL=$(echo "${Sitelist[j]}" | parse_attr "a" "href")
    FILE_URL=http://www.1channel.ch$FILE_URL
    echo "$FILE_URL"
  done
fi
unset i
unset j
unset Sitelist[@]
unset FILE_URL
unset pagenum
unset wastebin
unset linknum
unset stripurl
unset IFS
unset finallink
unset linkfinal
unset Links[@]
unset Sites[@]
unset veehd[@]
unset putlocker[@]
unset sockshare[@]
unset daclips[@]
unset zalaa[@]
unset filebox[@]
unset uploadc[@]
unset daclips[@]
unset stagevu[@]
unset vidbux[@]
unset nosvideo[@]
unset putlockerdwn
unset socksharedwn
unset URL
unset a
unset b
unset c
unset d
unset e
unset f
unset g
unset h
unset l
unset m
}